<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新闻页面的增加或修改页面</title>
    <script src="../../../../res/js/jquery-3.4.1.min.js"></script>
    <script src="../../../../res/layui/layui.js" charset="utf-8"></script>
    <script src="../../../../res/js/Tools.js"></script>
    <link rel="stylesheet" href="../../../../res/layui/css/layui.css" media="all">
    <link href="../../../../res/echarts/eleTree.css" rel="stylesheet"/>
    <script src="../../../../res/echarts/eleTree.js"></script>
    <link rel="shortcut icon" type="image/x-icon" href="../../../../res/images/favicon.ico">
    <script src="../../../../res/js/login_common.js" charset="utf-8"></script>
    <style type="text/css">
        body {
            padding: 10px;
        }
    </style>
    <script>
        $.ajaxSetup({
            async: false
        });
        $(function(){
            $(".layui-bg-red").click(function(){
                $("#tsone").html("");
                $("#tstwo").html("");
            });
            //图片1
            $("#btnone").click(function () {
                $("#fileOne").click();
                $("#fileOne").change(function(obj){
                    var fileInput = $('#fileOne').get(0).files[0];//获取input控件的file
                    if(fileInput){
                        var str=fileInput.name;
                            //取后缀名
                        $("#tsone").html(str.substring(0,4)+"..."+str.match(/\.([^\.]+)$/)[1]);
                    }
                });
            });
            //图片2
            $("#btntwo").click(function () {
                $("#fileTwo").click();
                $("#fileTwo").change(function(obj){
                    fileSize = $('#fileTwo').get(0).files[0].size/1024/1024;
                    if(fileSize>=5){
                        layer.msg("图片大小不能大于5MB");
                        return false;
                    }
                    var fileInput = $('#fileTwo').get(0).files[0];//获取input控件的file
                    if(fileInput){
                        var str=fileInput.name;
                        _fileName = str.substring(str.lastIndexOf(".") + 1).toLowerCase();
                        if(_fileName!="jpg" && _fileName!="png" && _fileName!="gif"){
                            layer.msg("不支持上传该类文件");
                            return false;
                        }else{
                            //取后缀名
                            $("#tstwo").html(str.substring(0,4)+"..."+str.match(/\.([^\.]+)$/)[1]);
                        }
                    }
                });
            });
        })
    </script>
</head>
<body>
<form class="layui-form layui-form-pane" id="formDId" action="" method="post" enctype="multipart/form-data">
    <input type="hidden" id="pkNewsId" name="pkNewsId">
    <input type="hidden" name="nSmallfileone" id="nSmallfileone">
    <input type="hidden" name="nSmallfiletwo" id="nSmallfiletwo">
    <div class="layui-form-item">
        <label class="layui-form-label">新闻标题</label>
        <div class="layui-input-block">
            <input type="text" name="newsTitle" id="newsTitle" lay-verify="required" autocomplete="off" maxlength="50" placeholder="请输入新闻标题(必填项)" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">新闻简要</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入新闻简要(必填项)" id="newsBrid" lay-verify="required"  maxlength="250" name="newsBrid" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">新闻内容</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入新闻内容(必填项)" id="newsBody" lay-verify="required" maxlength="1500" name="newsBody" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选择部门</label>
        <div class="layui-input-block">
            <select id="personnelId" name="personnelId" lay-filter="aihao" lay-verify="required" placeholder="选择部门(必填项)">
                <option value=""></option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">新闻类型</label>
        <div class="layui-input-block">
            <select id="newsTypeId" name="newsTypeId"  lay-filter="aihao" lay-verify="required " placeholder="新闻类型(必填项)">
                <option value=""></option>

            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">文档附件</label>
        <div class="layui-input-inline">
            <input type="file" name="fileOne" style="display: none" id="fileOne"  autocomplete="off"
                   class="layui-input">
            <button id="btnone"  type="button" class="layui-btn layui-btn-normal">上传文件</button>
            <a id="tsone" target="_blank" style="text-decoration:none"></a>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">新闻图片</label>
            <div class="layui-input-inline">
                <input type="file" name="fileTwo" style="display: none" id="fileTwo"  autocomplete="off"
                       class="layui-input">
                <button id="btntwo"  type="button" class="layui-btn layui-btn-normal">上传文件</button>
                <a id="tstwo" target="_blank" style="text-decoration:none"></a>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn site-demo-active" lay-submit="" lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary layui-bg-red">重置按钮</button>
        </div>
    </div>
</form>
<script>
    var houses;
    var form=null;
    layui.use(['form', 'layedit', 'laydate', 'layer'], function () {
        form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;

        var pkLogId = getQueryString("pkLogId");
        //监听提交
        form.on('submit(demo1)', function (data) {
            // 单击之后提交按钮不可选,防止重复提交
            var DISABLED = 'layui-btn-disabled';
            // 增加样式
            $('.site-demo-active').addClass(DISABLED);
            // 增加属性
            $('.site-demo-active').attr('disabled', 'disabled');
            var formData = new FormData(document.getElementById("formDId"));

            var url= "../../../../OaNewsAction/addOrUpdateNews";
            var data=postAjaxFile(url,formData);
            if (data.code === 1) {
                window.parent.location.reload();//刷新父页面
                const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
            }  else {
                layer.msg("新增失败");
            }
            return false;
        });

        //表单取值
        layui.$('#LAY-component-form-getval').on('click', function () {
            var data = form.val('example');
            alert(JSON.stringify(data));
        });

        //重新渲染表单函数
        function renderForm() {
            layui.use('form', function () {
                var form = layui.form(); //高版本建议把括号去掉，有的低版本，需要加()
                form.render();
            });
        }

        form.render();

        //加载部门下拉框
        loadType();
        //加载新闻类型下拉框
        newsType();
        //修改操作
        var ide = getQueryString("id");
        if(ide) {
            findById();//修改页面的赋值
            var str=$("#nSmallfileone").val();
            var strone=$("#nSmallfiletwo").val();
            if(str.length>1){
                $("#tsone").html(str.substring(0,13)+"。。。。。 ."+str.match(/\.([^\.]+)$/)[1]);
                $("#tsone").prop("href",str);
            }
            if(strone.length>1){
                $("#tstwo").html(strone.substring(0,13)+"。。。。。 ."+strone.match(/\.([^\.]+)$/)[1]);
                $("#tstwo").prop("href",strone);
            }
        }
    });

    function findById(){
        var id=getQueryString("id");
        var url="../../../../OaNewsAction/findById/"+id;
        var data=getAjaxById(url);
        $("#pkNewsId").val(data.data.pkNewsId);
        $("#pkNewsTypeId").val(data.data.pkNewsTypeId);
        $("#newsTitle").val(data.data.newsTitle);
        $("#newsBrid").val(data.data.newsBrid);
        $("#newsBody").val(data.data.newsBody);
        $("#personnelId").val(data.data.personnelId);
        $("#newsTypeId").val(data.data.newsTypeId);
        $("#nSmallfileone").val(data.data.newsFile);
        $("#nSmallfiletwo").val(data.data.newsImg);
        form.render('select');
    }

    //加载部门下拉框
    function loadType(){
        var url="../../../../OaDepartmentAction/departmentfindByAll";
        var data=getAjax(url,null);
        $.each(data.data,function(index,item){
            $("#personnelId").append("<option value='"+item.pkDepartmentId+"'>"+item.departmentName+"</option>");
        });
        form.render('select');
    }

    //加载新闻类型下拉框
    function newsType(){
        var url="../../../../NewsType/selectValid";
        var data=getAjax(url,null);
        $.each(data.data,function(index,item){
            $("#newsTypeId").append("<option value='"+item.pkNewsTypeId+"'>"+item.newsTypeName+"</option>");
        });
        form.render('select');


    }
</script>
</body>
</html>